<?php

class KeUniqueValidator implements KeBasicValidator
{
    public $columns;
    public $obj;

	/**
	 * @see        BasicValidator::isValid()
	 */
	public function isValid (ValidatorMap $map, $str)
	{
	  $column = $map->getColumn();
	  $columns = $map->getValue();
	  $columns = explode(",", $columns);

	  
	  $cri = new Criteria();
	  foreach($columns as $col)
	  {
         $c = $column->getTable()->getColumn($col);
         $method = "get".$c->getPhpName();
         $cri->add($c->getFullyQualifiedName(), $this->obj->$method(), Criteria::EQUAL);
      }
	   $isValid = false;
	 

	  try {

		  $table = $column->getTable()->getPhpName();
		  $cmd = sprintf('$isValid = %sPeer::doCount($cri) == 0;', $table);
		  eval($cmd);

	  } catch(PropelException $e) {
		
	  }
	  if(!$isValid)
     {
  	  foreach($columns as $col)
	  {
         $c = $column->getTable()->getColumn($col);
         $tbl = $column->getTable()->getName();
         $coln =  strtolower( $tbl.'{'.$col.'}');
         $coln = str_replace('.', '_', $coln);
         ErrorLog::reportarError('errors', $coln, $map->getMessage() );
      }
            
     }

	  return $isValid;
	}

    public function setColumns($cols)
    {
        $this->columns = $cols;
    }

    public function setObject($obj)
    {
        $this->obj = $obj;
    }
}
